package com.carol.lmsbe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carol.lmsbe.common.Result;
import com.carol.lmsbe.dto.*;
import com.carol.lmsbe.service.EquipmentScrapService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 设备报废控制器
 */
@RestController
@RequestMapping("/api/equipment-scrap")
@RequiredArgsConstructor
@Slf4j
public class EquipmentScrapController {

    private final EquipmentScrapService equipmentScrapService;

    /**
     * 分页查询设备报废记录
     */
    @GetMapping("/list")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<EquipmentScrapDTO>> listScrapRecords(EquipmentScrapQueryRequest request) {
        try {
            Page<EquipmentScrapDTO> result = equipmentScrapService.listScrapRecords(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询设备报废记录失败", e);
            return Result.error("查询设备报废记录失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取报废记录详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<EquipmentScrapDTO> getScrapRecordById(@PathVariable Long id) {
        try {
            EquipmentScrapDTO result = equipmentScrapService.getScrapRecordById(id);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取报废记录详情失败", e);
            return Result.error("获取报废记录详情失败: " + e.getMessage());
        }
    }

    /**
     * 创建设备报废记录
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<EquipmentScrapDTO> createScrapRecord(@Valid @RequestBody EquipmentScrapRequest request) {
        try {
            EquipmentScrapDTO result = equipmentScrapService.createScrapRecord(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("创建设备报废记录失败", e);
            return Result.error("创建设备报废记录失败: " + e.getMessage());
        }
    }

    /**
     * 更新设备报废记录
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<EquipmentScrapDTO> updateScrapRecord(@PathVariable Long id, @Valid @RequestBody EquipmentScrapUpdateRequest request) {
        try {
            request.setId(id);
            EquipmentScrapDTO result = equipmentScrapService.updateScrapRecord(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("更新设备报废记录失败", e);
            return Result.error("更新设备报废记录失败: " + e.getMessage());
        }
    }

    /**
     * 删除设备报废记录
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<Void> deleteScrapRecord(@PathVariable Long id) {
        try {
            equipmentScrapService.deleteScrapRecord(id);
            return Result.success();
        } catch (Exception e) {
            log.error("删除设备报废记录失败", e);
            return Result.error("删除设备报废记录失败: " + e.getMessage());
        }
    }

    /**
     * 获取设备报废统计
     */
    @GetMapping("/statistics")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<List<Map<String, Object>>> getScrapStatistics(EquipmentStatisticsRequest request) {
        try {
            List<Map<String, Object>> result = equipmentScrapService.getScrapStatistics(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取设备报废统计失败", e);
            return Result.error("获取设备报废统计失败: " + e.getMessage());
        }
    }

    /**
     * 获取我的报废记录
     */
    @GetMapping("/my-scraps")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER', 'LAB_MANAGER')")
    public Result<Page<EquipmentScrapDTO>> getMyScrapRecords(EquipmentScrapQueryRequest request) {
        try {
            Page<EquipmentScrapDTO> result = equipmentScrapService.getMyScrapRecords(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("获取我的报废记录失败", e);
            return Result.error("获取我的报废记录失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除报废记录
     */
    @DeleteMapping("/batch")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<Void> batchDeleteScrapRecords(@RequestBody List<Long> ids) {
        try {
            equipmentScrapService.batchDeleteScrapRecords(ids);
            return Result.success();
        } catch (Exception e) {
            log.error("批量删除报废记录失败", e);
            return Result.error("批量删除报废记录失败: " + e.getMessage());
        }
    }

    /**
     * 导出报废数据
     */
    @GetMapping("/export")
    @PreAuthorize("hasAnyRole('ADMIN', 'LEADER')")
    public Result<byte[]> exportScrapData(EquipmentScrapQueryRequest request) {
        try {
            byte[] result = equipmentScrapService.exportScrapData(request);
            return Result.success(result);
        } catch (Exception e) {
            log.error("导出报废数据失败", e);
            return Result.error("导出报废数据失败: " + e.getMessage());
        }
    }
}
